// pages/user/forgetPwd/forgetPwd.js
import { forgetPwd, sms } from '../../../api.js'
var app = getApp(); //获取App内部的信息
Page({
  /* 页面的初始数据*/
  data: {
    sendcode:false,
    colora:false,
    colorb:false,
    colorc:false,
    isAuth: true,
    phone: '',
    password: '',
    code: '',
    time: '发送验证码',//倒计时
    currentTime: 61,
    disabled: false
  },
  // focusa(e) {
  //   if (this.data.phone != "") {
  //     return
  //   }
  //   this.setData({
  //     phone: ""
  //   })
  // },
  // focusb(e) {
  //   if (this.data.password != "") {
  //     return
  //   }
  //   this.setData({
  //     password: ""
  //   })
  // },
  // focusc(e) {
  //   if (this.data.code != "") {
  //     return
  //   }
  //   this.setData({
  //     code: ""
  //   })
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
  onShow(){
  },
 
  sendCode() {
    console.log(this.data.sendcode)
    if (this.data.sendcode){
      return
    }
    this.setData({
      sendcode: true
    })
    var _this = this;
    var currentTime = _this.data.currentTime;
    sms({ phone: this.data.phone, template: 'sendpass' }).then(res => {
      wx.showToast({
        title: res.data.msg,
        duration: 1000,
        icon: 'none'
      })
      if (res.data.code == 666) {
      
        let interval = setInterval(function () {
          currentTime--;
          _this.setData({
            time: currentTime + '秒',
            disabled: true
          })
          if (currentTime <= 0) {
            clearInterval(interval)
            _this.setData({
              time: '重新发送',
              currentTime: 61,
              disabled: false,
              sendcode:false
            })
          }
        }, 1000)
      } else if (res.data.code == 111111) {
        this.setData({
          sendcode:false
        })
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        this.setData({
          sendcode:false
        })
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },
  login() {
    console.log("hsxicfa", this.data);
    forgetPwd({ phone: this.data.phone,phone_code:this.data.code,password: this.data.password }).then(res => {
    
      if (res.data.code == 666) {
        wx.showToast({
          title: '修改成功',
          icon: 'success',
          success:function(){
              wx.navigateBack({
                delta:1
              })
          }
        })
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      }else{
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
      }
    })
  },
  // 验证手机号与密码是否填写
  // validateParame(e) {
  //   let _this = this;
  //   let val = e.detail.value;
  //   if (!val) {
  //     wx.showToast({
  //       title: '手机号与密码必须填写',
  //       duration: 1000,
  //       icon: 'none'
  //     })
  //     return false;
  //   }
  //   return true;
  // },
  syncData(e) {
    let { key } = e.target.dataset;
    let val = e.detail.value;
    this.setData({
      [key]: val,
      colora:true
    })
    console.log('key', e)
  },
  syncDatb(e) {
    let { key } = e.target.dataset;
    let val = e.detail.value;
    this.setData({
      [key]: val,
      colorb:true
    })
    console.log('key', e)
  },
  syncDatc(e) {
    let { key } = e.target.dataset;
    let val = e.detail.value;
    this.setData({
      [key]: val,
      colorc:true
    })
    console.log('key', e)
  }

})